/*
 * Created by SharpDevelop.
 * User: daniel
 * Date: 12/25/2006
 * Time: 4:41 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;

namespace MonoDoku
{
	/// <summary>
	/// Description of Class1.
	/// </summary>
	public class Board
	{
		int[,] places;
		int[,] groups;
		
		
		//Later on, this may tear things apart for optimization's sake
		public Board( int[,] places, int[,] groups )
		{
			this.places = places;
			this.groups = groups;
		}
		
		public int[] get_row( Coords c ){
			int width = Utility.grid_width( places );
			int[] toReturn = new int[ width ];
			for( int i = 0; i < width; i++ ){
				toReturn[i] = places[i, c.y];
			}
			return toReturn;
		}
		
		
		public int[] get_col( Coords c ){
			int height = Utility.grid_height( places );
			int[] toReturn = new int[height];
			for(int i = 0; i < height; i++ ){
				toReturn[ i ] = places[ c.x, i ];
			}
			return toReturn;
		}
		
		//This is a very wasteful method. It would be more efficient to chop up
		//the gropus at method creation.
		public int[] get_grp( Coords c ){
			int counter = 0;
			int group = groups[c.x,c.y];
			int[] toReturn = new int[ group_size() ];
			int width = Utility.grid_width( groups );
			int height = Utility.grid_height( groups );
			
			for(int i = 0; i < width; i++ ){
				for( int j = 0; j < height; j++){
					if( groups[i,j] == group ){
						toReturn[counter] = places[i,j];
						counter++;
					}
				}
			}
			
			return toReturn;
			
		}
		
		public int[,] get_board(){
			return places;
		}
		
		public int[,] get_groups(){
			return groups;
		}
		
		public int group_size(){
			return Utility.grid_height(groups );
		}
		
		public void set_place( Coords c, int new_value ){
			places[c.x,c.y] = new_value;
		}
		
		public Stack free_spaces( ){
			int width = Utility.grid_width( groups );
			int height = Utility.grid_height( groups );
			
			Stack toReturn = new Stack();
			for( int x = 0; x < width; x++ ){
				for( int y = 0; y < height; y++ ){
					if( places[x,y] == 0 ){
						toReturn.Push( new Coords( x, y ) );
					}
				}
			}
			
			return toReturn;
		}
		
		public Board copy(){
			return new Board( Utility.grid_copy( places) , Utility.grid_copy( groups ) );
		}
		
		public bool solved( ){
			int width = Utility.grid_width( groups );
			int height = Utility.grid_height( groups );
			
			for(int i = 0; i < width; i++ ){
				for( int j = 0; j < height; j++){
					if( places[i,j] == 0) return false;
				}
			}
			
			return true;
		}
	}
}
